﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zcu.PanelComposer.Interfaces;
using System.Windows;
using System.IO;
using Zcu.PanelComposer.Models.Model;

namespace Zcu.PanelComposer.ViewModels.Core
{
    /// <summary>
    /// 
    /// </summary>
    public static class FileHelper
    {
        /// <summary>
        /// Name of Drawing directory.
        /// </summary>
        public static readonly string DrawingFolderConst = @"Drawing\";

        /// <summary>
        /// Gets the application dictonary.
        /// </summary>
        public static string ApplicationDictonary
        {
            get { return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
        }

        /// <summary>
        /// Gets the drawing dictonary.
        /// </summary>
        public static string DrawingDictonary
        {
            get { return Path.Combine(ApplicationDictonary, DrawingFolderConst); }
        }

        /// <summary>
        /// Gets the drawing path.
        /// </summary>
        /// <param name="drawing">The drawing.</param>
        /// <returns></returns>
        public static string GetDrawingPath(Drawing drawing)
        {
            return Path.Combine(DrawingDictonary, drawing.Path);
        }

        /// <summary>
        /// Saves the drawing.
        /// </summary>
        /// <param name="repository">The repository.</param>
        /// <param name="filename">The filename.</param>
        public static Drawing SaveDrawing(IRepository repository, string filename)
        {
            var guid = Guid.NewGuid();

            // vytvorime slozku dle dnesniho datumu
            string directory = Path.Combine(DrawingDictonary, DateTime.Today.ToString("ddMMyyyy"));
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string path = Path.Combine(directory, guid + Path.GetExtension(filename));

            // zkopirujeme obrazek a prejmenujeme na GUID
            File.Copy(filename, Path.Combine(directory, path));

            // vykres
            Drawing drw = new Drawing();
            drw.CreatedDate = DateTime.Now;
            drw.DisplayName = Path.GetFileName(filename);
            drw.Extension = Path.GetExtension(filename);
            drw.Hash = guid.ToString();
            drw.Path = path.Replace(DrawingDictonary, string.Empty);

            // vraceni vykresu
            return drw;
        }
    }
}
